local mumu = fk.CreateSkill{
  name = "kl__mumu",
}


mumu:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(mumu.name) and player.phase == Player.Play and
    (table.find(player.room.alive_players, function (p) return #p:getCardIds("e") > 0 end) or (#player.room:canMoveCardInBoard() > 0 and not player:isNude()))
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local choices = {}
    if #player.room:canMoveCardInBoard() > 0 and not player:isNude() then
      table.insert(choices,"kl__mumu_move")
    end
    if table.find(room.alive_players, function(p)
      return #p:getCardIds("e") > 0
    end) then
      table.insert(choices,"kl__mumu_get")
    end
    table.insert(choices,"Cancel")
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = mumu.name,
    })
    if choice ~= "Cancel" then
      event:setCostData(self, {choice = choice})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event:getCostData(self).choice == "kl__mumu_move" then
      if player:isNude() then return end
      room:askToDiscard(player, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = mumu.name,
        cancelable = false,
      })
      if #player.room:canMoveCardInBoard() == 0 then return end
      local targets = room:askToChooseToMoveCardInBoard(player, {
        skill_name = mumu.name,
        flag = nil,
        prompt = "#kl__mumu-move",
        cancelable = false,
      })
      room:askToMoveCardInBoard(player, {
        skill_name = mumu.name,
        flag = nil,
        target_one = targets[1],
        target_two = targets[2],
      })
    else
      local targets = table.filter(room.alive_players, function(p)
        return #p:getCardIds("e") > 0
      end)
      local to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = targets,
        skill_name = mumu.name,
        prompt = "#kl__mumu-get",
        cancelable = false,
      })[1]
      if #to:getCardIds("e") > 0 then
        local id = room:askToChooseCard(player, {
          target = to,
          flag = "e",
          skill_name = mumu.name,
          prompt = "#kl__mumu-prey",
        })
        room:moveCardTo(id, Card.PlayerHand, player, fk.ReasonPrey, mumu.name, nil, true, player)
      end
    end
  end,
})


return mumu
